package com.ray.test.hander;

import com.ray.test.config.CommonConfig;
import com.ray.test.core.ClassCore;
import com.ray.test.core.CoreResult;
import com.ray.test.core.InterfaceCore;
import com.ray.test.proxy.MockTestConfig;
import lombok.extern.slf4j.Slf4j;

/**
 * @author bo shen
 * @Description: spring 事务 处理
 * @Class: SpringTxHander
 * @Package com.ray.test.hander
 * @date 2019/12/12 15:14
 * @company <p>杭州传化陆鲸科技有限公司</p>
 * @updateRecord time(修改时间)  author(修改人)   desc(修改内容)
 */
@Slf4j
public class SpringTxHander {
    /***上下文容器**/
    private HanderContent handerContent;

    private ClassCore classCore = new ClassCore();
    private InterfaceCore interfaceCore  = new InterfaceCore();

    public SpringTxHander(HanderContent handerContent) {
        this.handerContent = handerContent;
    }

    public int runTest() {
        MockTestConfig.clearConfig();
        if (handerContent == null || handerContent.getRunClasss() == null) {
            log.info("单元测试执行结束，参数不合法");
            return 0;
        }

        log.info("当前执行数量:{}", handerContent.getRunClasss().size());
        Integer success = 0;
        Integer error = 0;
        for (Class<?> clazz : handerContent.getRunClasss()) {
            //过滤配置
            if (CommonConfig.isExclude(clazz.getName())) {
                continue;
            }
            CoreResult coreResult = null;
            if (clazz.isInterface()) {
                coreResult = interfaceCore.core(clazz, handerContent);
            } else {
                coreResult = classCore.core(clazz, handerContent);
            }
            //数量统计
            if (coreResult  != null ){
                success += coreResult.getSuccess();
                error+= coreResult.getError();
            }
        }
        log.info("单元测试执行结果，成功:{},失败:{}", success, error);
        return error;
    }
}
